function handleFileUpload(files) {
	for ( var i = 0; i < files.length; i++) {
		var fd = new FormData();
		fd.append('file', files[i]);
		sendFileToServer(fd);

	}
}
function sendFileToServer(formData) {

	var uploadURL = getBaseUrl() + '/receipts/recieptupload'; //Upload URL
	$('#spinter-uploading').show();
	$.ajax( {
		url : uploadURL,
		type : "POST",
		contentType : false,
		processData : false,
		cache : false,
		data : formData,
		dataType : "json",
		success : function(data) {
			if (data.status == 'OK') {
				var parrentPage = $('#parrent_page_name').val();
				if(parrentPage == 'expense_page') {
					loadContentAttachReceiptDialog();
				} else {
					$('#allReceiptsContainer').prepend(data.html);
				}
			} else {
				alert(data.messages);
			}
			$('#spinter-uploading').hide();
		}
	});

}
function loadContentAttachReceiptDialog() {
	var url = getBaseUrl() + '/expenses/loadcontentattachreceiptdialog';
	var values = $('#receipt_filter_form').serialize();
	$.ajax( {
		url : url,
		type : "POST",
		data : values,
		beforeSend: function(){
			hideSearchLoadingSpinner();
		},
		complete: function() {
			hideSearchLoadingSpinner();
		},		
		success : function(data) {
			$('#allReceiptsContainer').html(data);
		}
	});
}
$(document).ready(
		function() {
			var obj = $("#qq-upload-drop-area");
			obj.on('dragenter', function(e) {
				e.stopPropagation();
				e.preventDefault();
				$(this).css('border', '2px solid #0B85A1');
			});
			obj.on('dragover', function(e) {
				e.stopPropagation();
				e.preventDefault();
			});
			obj.on('drop', function(e) {
				$(this).css('border', '2px dotted #0B85A1');
				e.preventDefault();
				var files = e.originalEvent.dataTransfer.files;

				//We need to send dropped files to Server
				handleFileUpload(files);

			});

			$(document).on('dragenter', function(e) {
				e.stopPropagation();
				e.preventDefault();
			});
			$(document).on('dragover', function(e) {
				e.stopPropagation();
				e.preventDefault();
				obj.css('border', '2px dotted #0B85A1');
			});
			$(document).on('drop', function(e) {
				e.stopPropagation();
				e.preventDefault();
			});
			$(document).on(
					"click",
					"#receipts_selectAllCheckbox",
					function() {
						$('.receipts_selector_checkbox:input:checkbox').not(
								this).prop('checked', this.checked);
					});
			$(document).on("click", "#receipts_batchDelete", function() {
				var uploadURL = getBaseUrl() + '/receipts/delete'; //Upload URL
				var deleteString = '';
				$('input:checkbox.receipts_selector_checkbox').each(function() {
					var sThisVal = (this.checked ? $(this).val() : "");
					deleteString += sThisVal + '|';
				});
				$.ajax( {
					url : uploadURL,
					type : "POST",
					data : 'deleteString=' + deleteString,
					success : function(data) {
					}
				});
				$('input:checkbox.receipts_selector_checkbox').each(function() {
					$(this).parent().parent().parent().remove();
				});
			});

			$(document).on("click", ".delete_receipt_button", function() {
				var uploadURL = getBaseUrl() + '/receipts/deleteeach';
				var idString = $(this).attr('id');
				var idSplit = idString.split('_');
				var deleteId = idSplit[1];
				$.ajax( {
					url : uploadURL,
					type : "POST",
					data : 'delete_id=' + deleteId
				});
				$(this).parent().parent().parent().parent().remove();
			});
			$(document).on("click", "#btn_search", function() {
				var uploadURL = getBaseUrl() + '/receipts/search'; //Upload URL
				var values = $('#filter').serialize();
				$.ajax( {
					url : uploadURL,
					type : "POST",
					data : values,
					success : function(data) {
						$('#allReceiptsContainer').html(data);
					}
				});
			});
			$(document).on('click', '#button_importButton', function() {
				$('#file').click();
			});
			$(document).on('change', '#file', function() {
				var filedata = document.getElementById("file");
				handleFileUpload(filedata.files);
			});
			$(document).on('click', '#btn_reset', function() {
				$("#filter")[0].reset();
			});
			$(document).on(
					'click',
					'#receipts_addReport',
					function() {

						var reportId = $('#receipts_reportSelector').val();
						if (reportId == 0)
							return false;
						$('#addreport_spinner').show();
						var uploadURL = getBaseUrl()
								+ '/receipts/addreceiptstoreport';
						var receiptString = '';
						$('input:checkbox.receipts_selector_checkbox').each(
								function() {
									var sThisVal = (this.checked ? $(this)
											.val() : "");
									receiptString += sThisVal + '|';
								});

						$.ajax( {
							url : uploadURL,
							type : "POST",
							data : 'receiptString=' + receiptString
									+ '&reportId=' + reportId,
							success : function(data) {
								$('#addreport_spinner').hide();
							}
						});

					});

		});
